package grafica;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;



public class NetPlayPanel extends JPanel implements	ActionListener, MouseMotionListener, MouseListener{
	private Myframe gameFrame;
	private Toolkit t;
	private JTextField host;
	private JTextField porta;
	private JTextField user;
	private JLabel lab;
	private JLabel labHost;
	private JLabel labPorta;
	private JLabel labUser;
	private JButton conferma;
	private Color avviaColor;
	private Color partecipaColor;
	private boolean drawTitle;
	private boolean scelta;
	
	public NetPlayPanel(Myframe gameFrame){
		this.gameFrame = gameFrame;
		this.drawTitle = true;
		this.host = new JTextField();
		this.porta = new JTextField();
		this.user = new JTextField();
		this.lab = new JLabel();
		this.lab.setPreferredSize(new Dimension(300,400));
		this.labHost = new JLabel("Host");
		this.labPorta = new JLabel("Porta");
		this.labUser = new JLabel("User");
		this.conferma = new JButton("Conferma");
		this.conferma.setPreferredSize(new Dimension(100,40));
		this.labHost.setPreferredSize(new Dimension(70,35));
		this.labHost.setForeground(Color.WHITE);
		this.labPorta.setPreferredSize(new Dimension(70,35));
		this.labPorta.setForeground(Color.WHITE);
		this.labUser.setPreferredSize(new Dimension(70,35));
		this.labUser.setForeground(Color.WHITE);
		this.host.setPreferredSize(new Dimension(170,20));
		this.porta.setPreferredSize(new Dimension(170,20));
		this.user.setPreferredSize(new Dimension(170,20));
		t = Toolkit.getDefaultToolkit();
		this.avviaColor = Color.BLUE;
		this.partecipaColor = Color.BLUE;
		conferma.addActionListener(this);
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		this.setPreferredSize(new Dimension (300,200));
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponents(g);
		g.drawImage(t.getImage("images/Menu/spazio1.gif"),0, 0, null);
		if(drawTitle)
			g.drawImage(t.getImage("images/Menu/menuTitle.gif"), 80, 440, null);
		
		g.setColor(avviaColor);
		g.fillRoundRect(80, 90, 200, 70, 20, 30);
		g.setColor(partecipaColor);
		g.fillRoundRect(80, 210, 200, 70, 20, 30);
		g.setFont(gameFrame.getMenu().getMyFont());
		g.setColor(Color.WHITE);
		g.drawString("AVVIA",140,120);
		g.drawString("PARTITA",125,150);
		g.drawString("PARTECIPA",110,250);
		
	}

	public void mouseMoved(MouseEvent e) {
		if((e.getX()> 80)&& (e.getX() < 280)&&(e.getY()< 160) && (e.getY()> 90)){
			avviaColor = Color.CYAN;
			partecipaColor = Color.BLUE;
		}
		else if((e.getX()> 80)&& (e.getX() < 280)&&(e.getY()< 280) && (e.getY()> 210)){
			avviaColor = Color.BLUE;
			partecipaColor = Color.CYAN;
		}
		else{
			avviaColor = Color.BLUE;
			partecipaColor = Color.BLUE;
		}
		this.repaint();	
	}
	
	public void mousePressed(MouseEvent e) {
		if((e.getX()> 80)&& (e.getX() < 280)&&(e.getY()< 160) && (e.getY()> 90)){
			drawTitle = false;
			scelta = true;
			this.removeAll();
			this.add(lab);
			this.add(labPorta);
			this.add(porta);
			this.add(labUser);
			this.add(user);
			this.add(conferma);
			this.updateUI();
			
		}
		else if((e.getX()> 80)&& (e.getX() < 280)&&(e.getY()< 280) && (e.getY()> 210)){
			drawTitle = false;
			scelta = false;
			this.removeAll();
			this.add(lab);
			this.add(labHost);
			this.add(host);
			this.add(labPorta);
			this.add(porta);
			this.add(labUser);
			this.add(user);
			this.add(conferma);
			this.updateUI();
		}
		this.repaint();
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == conferma){
			NetFrame nf;
			if(scelta){
				ServerFrame server = new ServerFrame();
				nf = new NetFrame("localhost",Integer.parseInt(porta.getText()),user.getText());
			} else{
				nf = new NetFrame(host.getText(),Integer.parseInt(porta.getText()),user.getText());
			}
			nf.setVisible(true);
		}	
	}
	
	public void mouseDragged(MouseEvent e) {}
	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
}
